﻿'	CryptographyHelper & Cryptography .NET classes
'	http://maxvergelli.wordpress.com/

'	Copyright (c) 2010 Max Vergelli

'	Permission is hereby granted, free of charge, to any person obtaining a copy
'	of this software and associated documentation files (the "Software"), to deal
'	in the Software without restriction, including without limitation the rights
'	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
'	copies of the Software, and to permit persons to whom the Software is
'	furnished to do so, subject to the following conditions:

'	The above copyright notice and this permission notice shall be included in
'	all copies or substantial portions of the Software.

'	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
'	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
'	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
'	THE SOFTWARE.

Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Namespace Utils
    Public Class CryptographyHelper

        Public Enum EncodingBaseTypes As Integer
            Hex = 1
            Base64 = 2
        End Enum

        Public Enum HashTypes As Integer
            MD5 = 1
            SHA1 = 2
            SHA256 = 3
            SHA512 = 4
        End Enum

        Public Sub New(Optional ByVal encoding_type As Integer = EncodingBaseTypes.Hex)
            'by default encryption/hashing outputs hexadecimal strings and decryption get hexstrings as input
            If encoding_type <> EncodingBaseTypes.Hex And encoding_type <> EncodingBaseTypes.Base64 Then
                encoding_type = EncodingBaseTypes.Hex
            End If
            EncodingType = encoding_type
        End Sub

        Public Property EncodingType() As Integer

        Public Function GetHash(ByVal input As String, _
                Optional ByVal hash_type As Integer = HashTypes.MD5) As String

            Dim crypto As New Cryptography
            Dim bytes() As Byte = Nothing
            Select Case hash_type
                Case HashTypes.MD5
                    bytes = crypto.CreateMD5Hash(input)
                Case HashTypes.SHA1
                    bytes = crypto.CreateSHA1Hash(input)
                Case HashTypes.SHA256
                    bytes = crypto.CreateSHA256Hash(input)
                Case HashTypes.SHA512
                    bytes = crypto.CreateSHA512Hash(input)
                Case Else
                    bytes = crypto.CreateMD5Hash(input)
            End Select
            Dim hash As String = ""
            Select Case EncodingType
                Case EncodingBaseTypes.Hex
                    hash = crypto.BytesToHexString(bytes)
                Case EncodingBaseTypes.Base64
                    hash = crypto.BytesToBase64(bytes)
                Case Else
                    hash = crypto.BytesToHexString(bytes)
            End Select
            crypto = Nothing
            Return hash

        End Function

        Public Function Encrypt(ByVal input As String, _
                                ByVal password As String) As String

            Dim crypto As New Cryptography(password)
            Dim bytes() As Byte = crypto.Encrypt(input)
            Dim encrypted As String = ""
            If bytes IsNot Nothing Then
                Select Case EncodingType
                    Case EncodingBaseTypes.Hex
                        encrypted = crypto.BytesToHexString(bytes)
                    Case EncodingBaseTypes.Base64
                        encrypted = crypto.BytesToBase64(bytes)
                    Case Else
                        encrypted = crypto.BytesToHexString(bytes)
                End Select
            End If
            crypto = Nothing
            Return encrypted

        End Function

        Public Function Decrypt(ByVal input As String, _
                                ByVal password As String) As String

            Dim decrypted As String = ""
            If input.Length > 0 Then
                Dim crypto As New Cryptography(password)
                Dim enc_bytes() As Byte = Nothing
                Select Case EncodingType
                    Case EncodingBaseTypes.Hex
                        enc_bytes = crypto.HexStringToBytes(input)
                    Case EncodingBaseTypes.Base64
                        enc_bytes = crypto.Base64ToBytes(input)
                    Case Else
                        enc_bytes = crypto.HexStringToBytes(input)
                End Select
                decrypted = crypto.Decrypt(enc_bytes)
                crypto = Nothing
            End If
            Return decrypted

        End Function
    End Class
End Namespace